import 'package:cunyutech_flutter_ui/cunyutech_flutter_ui.dart';
import 'package:cunyutech_flutter_ui/src/button/main.dart';
import 'package:cunyutech_flutter_ui/src/theme/main.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class BSAction {
  BSAction({
    this.title,
    this.value,
  });
  String title;
  String value;
}


/// 取消是 -1 其它action操作是下标值
Future<int> showBSActionDialog(BuildContext context, List<PrimaryButtonProps> actions, { String title }  ) async {

  WidgetBuilder builder = cyThemeWrapBuilder((BuildContext _context) {
    return BSActionDialog(context: context, actions: actions, title: title);
  }, context);

  return await showModalBottomSheet<int>(
    context: context,
    backgroundColor: Colors.transparent,
    builder: builder
  );
}

class BSActionDialog extends StatefulWidget {

  static const double HeaderHeight = 44;
  static const double ActionHeight = 56;
  // static const Color BorderColor = CYColors.Gray;
  // static const Color DarkBorderColor = CYColors.Gray3;
  // static const Color DeleteActionColor = CYColors.Red;
  static const double Spacing = CYMargin.Small;

  BSActionDialog({Key key, this.context, this.actions, this.title }) : super(key: key);

  final List<PrimaryButtonProps> actions;
  final String title;
  final BuildContext context;

  _BSActionDialogState createState() => _BSActionDialogState();
}

class _BSActionDialogState extends State<BSActionDialog> {
  @override
  Widget build(BuildContext context) {
    var cs = CYThemeData.of(context);
    bool hasTitle = widget.title != null;
    double height = (widget.actions.length + 1) * BSActionDialog.ActionHeight + BSActionDialog.Spacing;
    Color backgroundColor = cs.brightness == Brightness.light ? cs.primaryBackColor :  cs.backColorSystem.secondaryColor;
    Color borderColor = cs.foreColorSystem.borderColor;
    Color titleColor = cs.foreColorSystem.placeholderColor;
    if(hasTitle) {
      height += BSActionDialog.HeaderHeight;
    }
    List<Widget> titles = widget.title != null ? 
    [Container(
      height: BSActionDialog.HeaderHeight,
      decoration: widget.actions.length > 0 ?  BoxDecoration(
        border: Border(bottom: BorderSide(color: borderColor))
      ) : null,
      child: CYParagraph(
        widget.title, 
        color: titleColor, 
        lineHeight: null, 
        alignment: Alignment.center,
        textAlign: TextAlign.center,
      )
    )] : [];

    List<Widget> actions = widget.actions.map((PrimaryButtonProps props){
      int index = widget.actions.indexOf(props);
      props.color ??= cs.linkColor;
      props.backgroundColor ??= Colors.transparent;
      props.borderRadius = 0;
      props.bold = false;
      props.onTap = () {
        Navigator.of(context).pop(widget.actions.indexOf(props));
      };
      return Container(
        child: PrimaryButton.withProps(props),
        decoration: widget.actions.length != index+1 ?  BoxDecoration(
          border: Border(bottom: BorderSide(color: borderColor))
        ) : null,
      );
    }).toList();

    return SafeArea(
      top: false,
      bottom: true,
      child: Container(
        height: height,
        child:  Container(
          child: Column(
            children: <Widget>[
              Container(
                decoration: BoxDecoration(
                  color: backgroundColor,
                  borderRadius: BorderRadius.circular(PrimaryButton.ButtonBorderRadius),
                ),
                margin: PrimaryButton.ButtonMargin,
                child: Column(
                  children: titles..addAll(actions)
                ),
              ),
              SizedBox(height: BSActionDialog.Spacing,),
              PrimaryButton(
                backgroundColor: backgroundColor, 
                title: "取消",
                bold: false,
                color: cs.linkColor,
                onTap: (){
                  Navigator.of(context).pop(-1);
                },
              )
            ]
          )
        ),
      )
    );
  }
}